package com.cls.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cls.yygh.common.result.Result;
import com.cls.yygh.common.utils.MD5;
import com.cls.yygh.hosp.service.HospitalSetService;
import com.cls.yygh.model.hosp.HospitalSet;
import com.cls.yygh.vo.hosp.HospitalSetQueryVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @program: yygh
 * @description:
 * @author: LH
 * @create: 2021-10-26 09:28
 * @Platform: JDK 1.8.0_301
 **/
@Slf4j
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@Api(tags = "医院管理设置")
public class HospitalSetController {

    //注入service
    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private ObjectMapper objectMapper;

    /**1、医院设置列表
     *
     * @return
     */
    @PostMapping("/findAll")
    @ApiOperation(value = "获取所有医院设置", notes = "获取所有医院设置", httpMethod = "POST",
            response = Result.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public Result getAllHospSet(){
        try{
            List<HospitalSet> hospitalSetList = hospitalSetService.list();
            return Result.ok(hospitalSetList);
        } catch (Exception e) {
            log.error("【/admin/hosp/hospitalSet/findAll-获取所有医院设置接口异常 】", e);
            e.printStackTrace();
            return Result.fail();
        }
    }

    /**
     *2、删除医院设置
     *
     *
     * */
    @PostMapping("/{id}")
    @ApiOperation(value = "逻辑删除医院设置", notes = "逻辑删除医院设置", httpMethod = "POST",
            response = Result.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "医院id",
                    dataType = "Long", paramType = "path",required = true)
    })
    public Result removeHospSet(@PathVariable Long id){
        try{
            boolean flag = hospitalSetService.removeById(id);
            if (flag){
                return Result.ok();
            }else {
                return Result.fail();
            }
        } catch (Exception e) {
            log.error("【/admin/hosp/hospitalSet/-逻辑删除医院设置接口异常 】", e);
            e.printStackTrace();
            return Result.fail();
        }

    }

    //3、条件查询带分页
    @PostMapping("/findPageHospSet/{current}/{limit}")
    @ApiOperation(value = "条件分页查询医院设置", notes = "条件分页查询医院设置", httpMethod = "POST",
            response = Result.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页",
                    dataType = "Long", paramType = "path",required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数",
                    dataType = "Long", paramType = "path",required = true),
            @ApiImplicitParam(name = "hospitalSetQueryVo", value = "医院查询条件",
                    dataType = "HospitalSetQueryVo", paramType = "body")
    })
    public Result findPageHospSet(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        String inputParam = "";
        try{
            inputParam = objectMapper.writeValueAsString(hospitalSetQueryVo);
            //创建page对象，传递当前页，每页记录
            Page<HospitalSet> page = new Page<>(current,limit);
            //构建条件
            QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
            String hosname = hospitalSetQueryVo.getHosname();   //医院名称、
            String hoscode = hospitalSetQueryVo.getHoscode();   //医院编号
            if(!StringUtils.isEmpty(hosname)){
                wrapper.like("hosname",hosname);
            }
            if (!StringUtils.isEmpty(hoscode)){
                wrapper.eq("hoscode",hoscode);
            }
            //调用方法实现分页查询
            Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page,wrapper);
            //返回结果
            return Result.ok(pageHospitalSet);
        } catch (Exception e) {
            log.error("【/admin/hosp/hospitalSet/findPageHospSet-接口入参 】：{}", inputParam);
            log.error("【/admin/hosp/hospitalSet/findPageHospSet-条件分页查询医院设置接口异常 】", e);
            e.printStackTrace();
            return Result.fail();
        }
    }

    //4、添加医院设置
    @PostMapping("/saveHospitalSet")
    @ApiOperation(value = "添加医院设置", notes = "添加医院设置", httpMethod = "POST",
            response = Result.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalSet", value = "医院设置实体",
                    dataType = "HospitalSet", paramType = "body", required = true)
    })
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        String inputParam = "";
        try{
            inputParam = objectMapper.writeValueAsString(hospitalSet);
            //设置状态 1 使用 0 不能使用
            hospitalSet.setStatus(1);
            //签名密钥
            Random random = new Random();
            hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
            //调用service
            boolean save = hospitalSetService.save(hospitalSet);
            if (save){
                return Result.ok();
            }else {
                return Result.fail();
            }
        } catch (Exception e) {
            log.error("【/admin/hosp/hospitalSet/saveHospitalSet-接口入参 】：{}", inputParam);
            log.error("【/admin/hosp/hospitalSet/saveHospitalSet-添加医院设置接口异常 】", e);
            e.printStackTrace();
            return Result.fail();
        }
    }

    //5、根据id获取医院设置
    @PostMapping("/getHospSet/{id}")
    @ApiOperation(value = "根据id获取医院设置", notes = "根据id获取医院设置", httpMethod = "POST",
            response = Result.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "医院id",
                    dataType = "Long", paramType = "path",required = true)
    })
    public Result getHospitalSetById(@PathVariable Long id){
        try{
            HospitalSet byId = hospitalSetService.getById(id);
            return Result.ok(byId);
        } catch (Exception e) {
            log.error("【/admin/hosp/hospitalSet/getHospitalSetById-根据id获取医院设置接口异常 】", e);
            e.printStackTrace();
            return Result.fail();
        }
    }

    //6、修改医院设置
    @PostMapping("/updateHospitalSet")
    @ApiOperation(value = "修改医院设置", notes = "修改医院设置", httpMethod = "POST",
            response = Result.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalSet", value = "医院设置实体",
                    dataType = "HospitalSet", paramType = "body", required = true)
    })
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        String inputParam = "";
        try{
            inputParam = objectMapper.writeValueAsString(hospitalSet);
            boolean flag = hospitalSetService.updateById(hospitalSet);
            if (flag){
                return Result.ok();
            }else{
                return Result.fail();
            }
        } catch (Exception e) {
            log.error("【/admin/hosp/hospitalSet/updateHospitalSet-接口入参 】：{}", inputParam);
            log.error("【/admin/hosp/hospitalSet/updateHospitalSet-修改医院设置接口异常 】", e);
            e.printStackTrace();
            return Result.fail();
        }
    }

    //7、批量删除医院设置
    @PostMapping("/batchRemove")
    @ApiOperation(value = "批量删除医院设置", notes = "批量删除医院设置", httpMethod = "POST",
            response = Result.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idList", value = "医院id集合",
                    dataType = "List<Long>", paramType = "body",required = true)
    })
    public Result updateHospitalSet(@RequestBody List<Long> idList){
        String inputParam = "";
        try{
            inputParam = objectMapper.writeValueAsString(idList);
            hospitalSetService.removeByIds(idList);
            return Result.ok();
        } catch (Exception e) {
            log.error("【/admin/hosp/hospitalSet/batchRemoveHospitalSet-接口入参 】：{}", inputParam);
            log.error("【/admin/hosp/hospitalSet/batchRemoveHospitalSet-批量删除医院设置接口异常 】", e);
            e.printStackTrace();
            return Result.fail();
        }
    }
}
